package com.voltmao.controller.module.intent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.persistent.model.SysUserInfo;
import com.voltmao.service.application.intent.IntentUserAgainServiceImpl;
import com.voltmao.service.application.intent.IntentUserServiceImpl;
import com.voltmao.service.application.sys.DictionaryServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.StringUtils;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 意向渠道控制层
 * @author zya
 */
@Controller
@RequestMapping(value = "/module/intent")
public class IntentController {
    @Autowired
    IntentUserServiceImpl intentUserService;
    @Autowired
    DictionaryServiceImpl dictionaryService;
    @Autowired
    IntentUserAgainServiceImpl intentUserAgainService;


    /**
     * 意向渠道完善信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/complete.json")
    @ResponseBody
    public ApiResult completeInfo(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                Map<String,Object> result = new HashMap<>();
                try {
                    intentUserService.completeInfo(params);
                }catch (Exception e){
                    apiResult.setStatus(Constants.FAIVAL);
                    e.printStackTrace();
                    result.put("error",e.getMessage());
                }

                return result;
            }
        });
    }

    /**
     * 获取渠道商个人信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/personal.json")
    @ResponseBody
    public ApiResult getPersonalInfo(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                SysUserInfo sysUserInfo = (SysUserInfo)request.getSession().getAttribute("sysUserInfo");
                if(sysUserInfo != null){
                    return intentUserService.getIntentInfo(sysUserInfo.getDataId());
                }else{
                    apiResult.setStatus(Constants.SESSION_TIMEOUT);
                    return null;
                }
            }
        });
    }

    /**
     * 根据provider_user的id获取意向渠道信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/getIntentInfo.json")
    @ResponseBody
    public ApiResult getIntentInfoById(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                if(StringUtils.isNotEmpty(params.get("providerUserId"))){
                    return intentUserService.getIntentInfoByProviderId(params.get("providerUserId")+"");
                }else{
                    apiResult.setStatus(Constants.PARAMETER_ERROR);
                    return null;
                }
            }
        });
    }
    /**
     * 未分配意向渠道的初始表格数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/unAssign.json")
    @ResponseBody
    public GridViewResult unAssignIntent(HttpServletRequest request){
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/searUnAssign.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                return intentUserService.selectUnAssign(params);
            }

            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                //需要渠道分类下拉框，区域下拉框
                Map<String,Object> res = new HashMap<>(8);
                res.put("qdlx",dictionaryService.getDictionaryByPCode("qdlx"));
                return res;
            }
        });
    }

    /**
     * 按条件查询未分配意向渠道列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/searUnAssign.json")
    @ResponseBody
    public GridViewResult unAssignSearch(HttpServletRequest request){
        return new DTOSmith(request).builderSeachGridResult(new DTOSmith.SeachGridBackCall() {

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                apiResult.dataUrl = "/searUnAssign.json";
                return intentUserService.selectUnAssign(params);
            }
        });
    }

    /**
     * 分配意向渠道给业务员
     * @param request
     * @return
     */
    @RequestMapping(value = "/assign.json")
    @ResponseBody
    public ApiResult Assign(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                try{
                    intentUserService.assign(params);
                    apiResult.setStatus(Constants.SUCCESS);
                }catch (Exception e){
                    apiResult.setStatus(Constants.PARAMETER_ERROR);
                    return e.getMessage();
                }

                return null;
            }
        });
    }

    /**
     * 重新分配意向渠道
     * @param request
     * @return
     */
    @RequestMapping(value = "/reAssign.json")
    @ResponseBody
    public ApiResult reAssign(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                try{
                    intentUserService.reAssign(params);
                }catch (Exception e){
                    apiResult.setStatus(Constants.PARAMETER_ERROR);
                    return e.getMessage();
                }
                return null;
            }
        });
    }

    /**
     * 业务员获取自己负责的意向渠道列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/intentForEmpl.json")
    @ResponseBody
    public GridViewResult intentForEmpl(HttpServletRequest request){
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/intentForEmplSear.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                return intentUserService.intentForEmpl(params);

            }

            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                //需要区域下拉，渠道商分类下拉，渠道等级下拉，跟进状态下拉
                Map<String, Object> res = new HashMap<>(8);
                res.put("qdlx",dictionaryService.getDictionaryByPCode("qdlx"));
                res.put("区域下拉","");
                res.put("qdsdj",dictionaryService.getDictionaryByPCode("qdsdj"));
                res.put("yxyhzt",dictionaryService.getDictionaryByPCode("yxyhzt"));
                return res;
            }
        });
    }

    /**
     * 业务员查询自己负责的渠道信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/searIntentForEmpl.json")
    @ResponseBody
    public GridViewResult searIntentForEmpl(HttpServletRequest request){
        return new DTOSmith(request).builderSeachGridResult(new DTOSmith.SeachGridBackCall() {
            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                apiResult.dataUrl="/searIntentForEmpl.json";
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                return intentUserService.intentForEmpl(params);
            }
        });
    }

    /**
     * 业务员查询自己负责的渠道信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/intentForEmplContractSelect.json")
    @ResponseBody
    public GridViewResult intentForEmplContractSelect(HttpServletRequest request){
        return new DTOSmith(request).builderSeachGridResult(new DTOSmith.SeachGridBackCall() {
            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                apiResult.dataUrl="/intentForEmplContractSelect.json";
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                return intentUserService.intentForEmplContractSelect(params);
            }
        });
    }

    /**
     * 业务员认证意向渠道信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/auditIntent.json")
    @ResponseBody
    public ApiResult auditChannel(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);

                try{
                    return intentUserService.aduitIntent(params);
                }catch (Exception e){
                    apiResult.setStatus(Constants.PARAMETER_ERROR);
                    apiResult.msg = e.getMessage();
                }
                return null;
            }
        });
    }

    /**
     * 总监获取所有已分配意向渠道的跟进信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/loadIntentFollowInfo.json")
    @ResponseBody
    public GridViewResult loadIntentFollowInfo(HttpServletRequest request){
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/searIntentFollowInfo.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                return intentUserService.getIntentFollowInfo(params);
            }

            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                //配置查询条件下拉
                Map<String, Object> res = new HashMap<>(8);
                res.put("区域下拉","待绑定");
                res.put("qdlx",dictionaryService.getDictionaryByPCode("qdlx"));
                res.put("qdsdj",dictionaryService.getDictionaryByPCode("qdsdj"));
                res.put("yxyhzt",dictionaryService.getDictionaryByPCode("yxyhzt"));
                return res;
            }
        });
    }

    /**
     * 总监查询意向渠道跟进状态
     * @param request
     * @return
     */
    @RequestMapping(value = "/searIntentFollowInfo.json")
    @ResponseBody
    public GridViewResult searIntentFollowInfo(HttpServletRequest request){
        return new DTOSmith(request).builderSeachGridResult(new DTOSmith.SeachGridBackCall() {
            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                apiResult.dataUrl="/searIntentFollowInfo.json";
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                return intentUserService.getIntentFollowInfo(params);
            }
        });
    }

    /**
     * 总监获取正式渠道的跟进信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/loadRealProviderFollowInfo.json")
    @ResponseBody
    public GridViewResult loadRealProviderFollowInfo(HttpServletRequest request){
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/searRealProviderFollowInfo.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                return intentUserService.getRealProviderFollowInfo(params);
            }

            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                //配置查询条件下拉
                Map<String, Object> res = new HashMap<>(8);
                res.put("区域下拉","待绑定");
                res.put("qdlx",dictionaryService.getDictionaryByPCode("qdlx"));
                res.put("qdsdj",dictionaryService.getDictionaryByPCode("qdsdj"));
                return res;
            }
        });
    }

    /**
     * 总监查询正式渠道信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/searRealProviderFollowInfo.json")
    @ResponseBody
    public GridViewResult searRealProviderFollowInfo(HttpServletRequest request){
        return new DTOSmith(request).builderSeachGridResult(new DTOSmith.SeachGridBackCall() {
            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                apiResult.dataUrl="/searRealProviderFollowInfo.json";
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                return intentUserService.getRealProviderFollowInfo(params);
            }
        });
    }


}
